package com.example.administrator.waitan.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.example.administrator.waitan.mygreendao.DaoMaster;
import com.example.administrator.waitan.mygreendao.DaoSession;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2016/1/19.
 */
public class BaseApplication extends Application {
    private static Context context;
    private static DaoSession daoSession;
    private static DaoMaster daoMaster;

    public static DaoMaster getDaoMaster() {
        if (daoMaster == null){
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context,"comicHome.db",null);
            daoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        daoSession = daoMaster.newSession();

        return daoMaster;
    }

    public static DaoSession getDaoSession() {
        if (daoSession == null){
            daoMaster = getDaoMaster();
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
            JPushInterface.init(this);
        JPushInterface.setDebugMode(true);
}

    public static Context getContext() {
        return context;
    }

    //用于存放我们所有activity的数组
    public static List<Activity> activities;

    //向集合中添加一个activity
    public static void addActivity(Activity activity){
        if(activities == null){
//如果集合为空 则初始化
            activities = new ArrayList<>();
        }
//将activity加入到集合中
        activities.add(activity);
    }

    //结束掉所有的Activity
    public static void finishAll(){
//循环集合, 将所有的activity finish
        for(Activity activity : activities){
            if(! activity.isFinishing()){
                activity.finish();
            }
        }
    }


}
